#include "lcd_init.h"
#include "stdio.h"
#include <string.h>
// #include "app.h"

TIM_HandleTypeDef htim1;
typedef void (*FunType)(void);
//**********************************write function begin********************************************
void wr_cmd_4spi_8bcs(uint8_t par)
{
    uint8_t i = 0;
    LCD_DCXLow();
    LCD_CSLow();
    for (i = 0; i < 8; i++)
    {
        LCD_SCLLow();
        if (par & 0x80)
        {
            LCD_SDAHigh();
        }
        else
        {
            LCD_SDALow();
        }
        LCD_SCLHigh();
        par <<= 1;
    }
    LCD_CSHigh();
    LCD_DCXHigh();
}
void wr_dat_4spi_8bcs(uint8_t par)
{
    uint8_t i = 0;
    LCD_DCXHigh();
    LCD_CSLow();
    for (i = 0; i < 8; i++)
    {
        LCD_SCLLow();
        if (par & 0x80)
        {
            LCD_SDAHigh();
        }
        else
        {
            LCD_SDALow();
        }
        LCD_SCLHigh();
        par <<= 1;
    }
    LCD_CSHigh();
    LCD_DCXHigh();
}
//**********************************read function begin********************************************
void Write(uint8_t dc, uint8_t par)
{
    if (dc == Command)
    {
        wr_cmd_4spi_8bcs(par);
    }
    else if (dc == Parameter)
    {
        wr_dat_4spi_8bcs(par);
    }
}

void HW_Reset(void)
{
    LCD_RSTHigh();
    HAL_Delay(1);
    LCD_RSTLow();
    HAL_Delay(10);
    LCD_RSTHigh();
    HAL_Delay(120);
}

void Delay(uint32_t ms)
{
    HAL_Delay(ms);
}

void SPI_WriteComm(uint8_t cmd)
{
    wr_cmd_4spi_8bcs(cmd);
}

void SPI_WriteData(uint8_t dat)
{
    wr_dat_4spi_8bcs(dat);
}

void initial(void)
{
    HW_Reset();

    SPI_WriteComm(0xCF);
    SPI_WriteData(0x00);
    SPI_WriteData(0x91);
    SPI_WriteData(0x30);
    
    SPI_WriteComm(0xED);
    SPI_WriteData(0x64);
    SPI_WriteData(0x03);
    SPI_WriteData(0x12);
    SPI_WriteData(0x81);
    
    SPI_WriteComm(0xE8);
    SPI_WriteData(0x85);
    SPI_WriteData(0x10);
    SPI_WriteData(0x78);
    
    SPI_WriteComm(0xCB);
    SPI_WriteData(0x39);
    SPI_WriteData(0x2C);
    SPI_WriteData(0x00);
    SPI_WriteData(0x34);
    SPI_WriteData(0x02);
    
    SPI_WriteComm(0xF7);
    SPI_WriteData(0x20);
    
    SPI_WriteComm(0xEA);
    SPI_WriteData(0x00);
    SPI_WriteData(0x00);
    
    SPI_WriteComm(0xC0); //Power control
    SPI_WriteData(0x21); //VRH[5:0]
    
    SPI_WriteComm(0xC1); //Power control
    SPI_WriteData(0x11); //SAP[2:0];BT[3:0]
    
    SPI_WriteComm(0xC5); //VCM control
    SPI_WriteData(0x3F);//3F
    SPI_WriteData(0x41);//3C
    
    SPI_WriteComm(0x36); // Memory Access Control
    SPI_WriteData(0x08);
    
    SPI_WriteComm(0x3A);
    SPI_WriteData(0x55);
    
    
    
    
    SPI_WriteComm(0xB1);
    SPI_WriteData(0x00);
    SPI_WriteData(0x1B);
    
    
    SPI_WriteComm(0xB6); // Display Function Control
    SPI_WriteData(0x0A);
    SPI_WriteData(0x02);
    
    SPI_WriteComm(0xF2); // 3Gamma Function Disable
    SPI_WriteData(0x00);
    
    SPI_WriteComm(0x26); //Gamma curve selected
    SPI_WriteData(0x01);
    
    SPI_WriteComm(0xE0); //Set Gamma
    SPI_WriteData(0x0F);
    SPI_WriteData(0x23);
    SPI_WriteData(0x1F);
    SPI_WriteData(0x0B);
    SPI_WriteData(0x0E);
    SPI_WriteData(0x08);
    SPI_WriteData(0x4B);
    SPI_WriteData(0xA8);
    SPI_WriteData(0x3B);
    SPI_WriteData(0x0A);
    SPI_WriteData(0x14);
    SPI_WriteData(0x06);
    SPI_WriteData(0x10);
    SPI_WriteData(0x09);
    SPI_WriteData(0x00);
    
    SPI_WriteComm(0xE1); //Set Gamma
    SPI_WriteData(0x00);
    SPI_WriteData(0x1C);
    SPI_WriteData(0x20);
    SPI_WriteData(0x04);
    SPI_WriteData(0x10);
    SPI_WriteData(0x08);
    SPI_WriteData(0x34);
    SPI_WriteData(0x47);
    SPI_WriteData(0x44);
    SPI_WriteData(0x05);
    SPI_WriteData(0x0B);
    SPI_WriteData(0x09);
    SPI_WriteData(0x2F);
    SPI_WriteData(0x36);
    SPI_WriteData(0x0F);
    
    //Write_LCD_REG(0x0000,0x21);
    
    SPI_WriteComm(0x11);//ExitSleep
    Delay(1200);
    SPI_WriteComm(0x29);//Displayon
    
    SPI_WriteComm(0x2C);
    
}

void keywait(uint32_t time)
{
    uint32_t i = 0;
    int pausing = 1;
    for (i = 0; i < time; i++)
    {
        if (HAL_GPIO_ReadPin(PAUSE_PA5_GPIO_Port, PAUSE_PA5_Pin) == GPIO_PIN_RESET)
        {
            HAL_Delay(20);
            if (HAL_GPIO_ReadPin(PAUSE_PA5_GPIO_Port, PAUSE_PA5_Pin) == GPIO_PIN_RESET)
            {
                while (HAL_GPIO_ReadPin(PAUSE_PA5_GPIO_Port, PAUSE_PA5_Pin) == GPIO_PIN_RESET)
                {
                    HAL_Delay(100);
                    printf("wait release\n\r");
                }
                printf("get key\n\r");
                while (pausing != 0)
                {
                    printf("pausing\n\r");
                    HAL_Delay(20);
                    if (HAL_GPIO_ReadPin(PAUSE_PA5_GPIO_Port, PAUSE_PA5_Pin) == GPIO_PIN_RESET)
                    {
                        printf("get 2nd key\n\r");
                        i = time - 2;
                        pausing = 0;
                        break;
                    }
                }
            }
        }
        HAL_Delay(1);
    }
}

void disp_main(void)
{
    sdcard_init();
    HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1); // 启动PWM
    __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 1000);
    while (1)
    {
        bmpDrawFile("1.bmp");
        keywait(Interval);
        bmpDrawFile("2.bmp");
        keywait(Interval);
        //__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 500);
        //keywait(Interval);
        //__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 1000);
        bmpDrawFile("3.bmp");
        keywait(Interval);
        bmpDrawFile("4.bmp");
        keywait(Interval);
        bmpDrawFile("5.bmp");
        keywait(Interval);
        bmpDrawFile("6.bmp");
        keywait(Interval);
        bmpDrawFile("7.bmp");
        keywait(Interval);
        bmpDrawFile("8.bmp");
        keywait(Interval);
        bmpDrawFile("9.bmp");
        keywait(Interval);
        bmpDrawFile("10.bmp");
        keywait(Interval);
    }
}